let axios = require('axios');
let { randomString } = require("../utils/string")
const access_token = 'b08ba21f610f259121f0b9e1acee7c3e'
module.exports = {
    async Pupload(req, resp) {
        if(req.body.content.length*0.75>2000000){resp.send({code:-2,"message": "文件过大"});return;}
        if (!req.query.fileName) { resp.send({ code: -2 }); return; }

        let FormData = require('form-data');
        let data = new FormData();
        data.append('access_token', access_token);
        data.append('content', req.body.content);
        data.append('message', 'upload');
        
        
        let d = new Date()
        let url = 'https://gitee.com/api/v5/repos/bendantada/img/contents/' + `${d.getFullYear()}-${d.getMonth()}-${d.getDate()}/` + randomString(6) + encodeURIComponent(req.query.fileName)
        
        let config = {
            method: 'post',
            url,
            headers: {
                'Accept': '*/*',
                'Host': 'gitee.com',
                'Connection': 'keep-alive',
                'Content-Type': 'multipart/form-data; boundary=--------------------------178579882712896047005388',
                'Cookie': 'BEC=1f1759df3ccd099821dcf0da6feb0357',
                ...data.getHeaders()
            },
            data: data
        };

        let re = await axios(config)
        await database.imgs.create({ url: re.data.content.download_url, name: fileName, fromid: req.query.userid, msgid: req.query.msgid, msgtype: req.query.msgtype })
        resp.send({ msg: re.data })
    }
}